package td.pt.widget;

import java.util.Comparator;

import td.domain.TagThought;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.ProvidesKey;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.PopupViewImpl;

public class TagListingPopupView extends PopupViewImpl implements
		TagListingPopupPresenter.MyView {

	private final Widget widget;

	public interface Binder extends UiBinder<Widget, TagListingPopupView> {
	}

	/**
	 * The main DataGrid.
	 */
	@UiField(provided = true)
	CellTable<TagThought> cellTable;

	/**
	 * The pager used to change the range of data.
	 */
	@UiField(provided = true)
	SimplePager pager;

	@UiField
	Label title;

	@UiField
	Button close;

	public static final ProvidesKey<TagThought> KEY_PROVIDER = new ProvidesKey<TagThought>() {
		@Override
		public Object getKey(TagThought item) {
			return item == null ? null : item.getId();
		}
	};

	private ListDataProvider<TagThought> dataProvider = new ListDataProvider<TagThought>();

	// Column<Thought, Hyperlink> tagDefColumn = new Column<TagThought,
	// Hyperlink>(
	// new AbstractCell<Hyperlink>() {
	//
	// @Override
	// public void render(
	// com.google.gwt.cell.client.Cell.Context context,
	// Hyperlink value, SafeHtmlBuilder sb) {
	// sb.append(SafeHtmlUtils.fromTrustedString(value.toString()));
	// }
	// }) {
	// @Override
	// public Hyperlink getValue(TagThought thought) {
	// String text = thought.getTitle();
	// String targetHistoryToken = "!detail;name=" + thought.getId();
	// return new Hyperlink(text, targetHistoryToken);
	// }
	// };

	Column<TagThought, String> tagNameColumn = new Column<TagThought, String>(
			new AbstractCell<String>() {

				@Override
				public void render(
						com.google.gwt.cell.client.Cell.Context context,
						String value, SafeHtmlBuilder sb) {
					sb.appendEscaped(value);
				}
			}) {
		@Override
		public String getValue(TagThought tagThought) {
			return tagThought.getTag().getName();
		}
	};

	Column<TagThought, Long> numTaggingsColumn = new Column<TagThought, Long>(
			new AbstractCell<Long>() {

				@Override
				public void render(
						com.google.gwt.cell.client.Cell.Context context,
						Long value, SafeHtmlBuilder sb) {
					sb.appendEscaped(value.toString());
				}
			}) {
		@Override
		public Long getValue(TagThought tagThought) {
			return tagThought.getCount();
		}
	};

	@Inject
	public TagListingPopupView(final EventBus eventBus, final Binder binder) {
		super(eventBus);
		cellTable = new CellTable<TagThought>(KEY_PROVIDER);
		cellTable.setWidth("100%", true);

		ListHandler<TagThought> sortHandler = new ListHandler<TagThought>(
				dataProvider.getList());
		cellTable.addColumnSortHandler(sortHandler);

		pager = new SimplePager(TextLocation.CENTER);
		pager.setDisplay(cellTable);
		dataProvider.addDataDisplay(cellTable);

		initTableColumns(cellTable, sortHandler);

		widget = binder.createAndBindUi(this);
	}

	/**
	 * Add the columns to the table.
	 */
	private void initTableColumns(CellTable<TagThought> cellTable,
			ListHandler<TagThought> sortHandler) {

		tagNameColumn.setSortable(true);
		sortHandler.setComparator(tagNameColumn, new Comparator<TagThought>() {
			@Override
			public int compare(TagThought o1, TagThought o2) {
				return o1.getTag().getName().compareTo(o2.getTag().getName());
			}
		});
		cellTable.addColumn(tagNameColumn, "Name");
		cellTable.setColumnWidth(tagNameColumn, 70, Unit.PCT);

		numTaggingsColumn.setSortable(true);
		sortHandler.setComparator(numTaggingsColumn,
				new Comparator<TagThought>() {
					@Override
					public int compare(TagThought o1, TagThought o2) {
						return o1.getCount().compareTo(o2.getCount());
					}
				});
		cellTable.addColumn(numTaggingsColumn, "# Taggings");
		cellTable.setColumnWidth(numTaggingsColumn, 30, Unit.PCT);
	}

	@Override
	public Widget asWidget() {
		return widget;
	}

	public Label getTitle() {
		return title;
	}

	public CellTable<TagThought> getCellTable() {
		return cellTable;
	}

	public SimplePager getPager() {
		return pager;
	}

	public void setCellTable(CellTable<TagThought> cellTable) {
		this.cellTable = cellTable;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public void refresh() {
		dataProvider.refresh();
	}

	public Button getClose() {
		return close;
	}

	public ListDataProvider<TagThought> getDataProvider() {
		return dataProvider;
	}

}
